#include "fact.hpp"

Fact::Fact()
{
  this->_type = FACT;
}

void			Fact::setState(state st)
{
  this->_state = st;
}

std::string const &	Fact::getName() const
{
  return this->_name;
}

void			Fact::setName(std::string const & name)
{
  this->_name = name;
}

void			Fact::solve()
{
  if (this->_state == UNDEFINED)
    {
      AObject* elem;

      for (unsigned int i = 0; i < this->_cond.size(); ++i)
	{
	  elem = this->_cond.front();
	  this->_cond.pop();
	  elem->solve();
	  if (elem->getState() != UNDEFINED)
	    {
	      if (this->_state != elem->getState() && this->_state != UNDEFINED)
		{
		  std::cout << "Confliting rules detected" << std::endl;
		  this->UsersChoice();
		}
	      else
		{
		  this->_state = elem->getState();
		  std::cout << this->_name << " is solved, the state is : " << this->_state << std::endl;
		}
	    }
	  this->_cond.push(elem);
	}
      if (this->_state == UNDEFINED)
	{
	  std::cout << this->_name << " is still UNDEFINED." << std::endl;
	  this->UsersChoice();
	}
    }
}

void			Fact::UsersChoice()
{
  bool flag = 1;
  std::string temp;

  do
    {
      std::cout << "Choose the state for " << this->_name << std::endl;
      std::cin >> temp;
      if (temp == "TRUE")
	{
	  this->_state = TRUE;
	  flag = 0;
	}
      else if (temp == "FALSE")
	{
	  this->_state = FALSE;
	  flag = 0;
	}
      if (flag)
	std::cerr << "ERROR, please write TRUE or FALSE"<< std:: endl;
    } while (flag);
}

bool			Fact::behaviour(LinkChain, LinkChainIterator, LinkChainIterator, LinkChainIterator)
{
  return false;
}
